package com.ydgk.service;

import com.ydgk.bean.Commodity;
import com.ydgk.dao.CommDao;

import java.util.HashMap;
import java.util.List;

public class CommService {
    CommDao commDao=new CommDao();

    public int deleteById(int id){
        return commDao.deleteById(id);
    }

    public HashMap commPaging(int pageNumber){
        int maxPage = commDao.maxPage("commodity");//求最大页
        if(pageNumber > maxPage){
            pageNumber = maxPage ;
        }
        if( pageNumber <1){
            pageNumber=1;
        }

        //生成分页的代码
        String pageCode = commDao.genPagination
                ("CommServlet.action",maxPage,pageNumber,"flag=findAll");

        List<Commodity> commodityList = commDao.findAll();

        HashMap map = new HashMap();
        map.put("pageCode" , pageCode);
        map.put("commodityList" , commodityList);
        return map;
    }

    public int save(Commodity commodity){
        return commDao.save(commodity);
    }

    public int updateById(Commodity commodity){
        return commDao.updateById(commodity);
    }

    public List<Commodity> findAll(){

        return commDao.findAll();
    }

    public Commodity findById(int id){return commDao.findById(id);}

    public int updateCnum(int num,int cid){
        return commDao.updateCnum(num, cid);
    }

}
